<template>
  <div>
     <!-- ref注册引用信息 -->
    <el-form
      :rules="rules"
      ref="loginForm"
      :model="loginForm"
      class="loginContainer"
    >
      <h3 class="loginTitle">用户权限管理系统</h3>
      <!-- prop="username"对应下面rules的校验 -->
      <el-form-item prop="username">
        <el-input
          type="text"
          v-model="loginForm.username"
          auto-complete="false"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="loginForm.password"
          auto-complete="false"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-col :span="16">
          <el-input
            id="code"
            type="text"
            v-model="loginForm.code"
            auto-complete="false"
            placeholder="点击图片更换验证码"
          ></el-input>
        </el-col>
        <el-col :span="2">
          <img class="captchaImg" :src="captchaUrl" @click="updateCaptcha" />
        </el-col>
      </el-form-item>
      <el-checkbox v-model="checked" class="remenber">记住我</el-checkbox>
      <el-button type="primary" style="width: 100%" @click="submitLogin"
        >登录</el-button
      >
    </el-form>
  </div>
</template>

<script>

import {mapMutations} from "vuex";
export default {
  name: "login",
  data() {
    return {
      captchaUrl: "login/getVerify?time=" + new Date(),
      userToken:'',
      loginForm: {
        username: "userAdmin",
        password: "666666",
        code: "",
      },
      checked: true,
      //校验输入框   trigger:'blur'失去焦点时触发
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      },
    };
  },

  methods: {
     ...mapMutations(['changeLogin']),
    updateCaptcha() {
      this.captchaUrl = "login/getVerify?time=" + new Date();
    },
    submitLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // 调用封装的post请求
          this.postRequest("/user/login", this.loginForm).then((resp) => {
            if (resp) {
              //  push和replace的区别是用了push浏览器的回退键可以回退
              // 存入用户信息到session后跳转页面
              window.sessionStorage.setItem('user',JSON.stringify(resp));
              this.userToken = resp.token;
              // 将用户token保存到vuex中
               this.changeLogin({ Authorization:this.userToken });
              this.$router.replace("/home");
            }
          });
          // 以下是未封装post请求的调用方式
          // this.$axios({
          //   method: "post",
          //   url: "/user/login",
          //   params: this.loginForm,
          // }).then((resp) => {
          //   alert(JSON.stringify(resp));
          // });
        } else {
          this.$message.error("内容不能为空哦");
          return false;
        }
      });
    },
  },
};
</script>

<style>
.loginContainer {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 15px 35px 15px 35px;
  background: #ffffff;
  border: 1px solid;
  box-shadow: 0 0 25px;
}
.loginTitle {
  margin-left: 80px;
}
#code {
  display: flex;
  width: 230px;
  margin-right: 5px;
}
.remenber {
  margin: 0px 0px 20px 0px;
}
</style>
